﻿/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : club_my.js                                 
*  Description  : 회원가입 
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-05-04  이윤노    Created.
*  2012-05-04  이윤노    Complete.
*******************************************************************************/

jQuery(function($){
	
	//일일 알림
	function clsClubNotificationList(){
		this._HTMLDom = null;
		this.listData = null;
		this.a = null;
		this._init();
	}	
	
	clsClubNotificationList.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content article.club_myclub div.club_myclub_result").css("min-height", "100px");
			
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("tbody tr:first").clone(true)
				, "$count": $layer.find("p.group span:eq(0) em")
				, "$empty": $("body").find("article.board_list_none:eq(0)").html("등록된 글이 없습니다.")
			}			
			this.a = true;
			this.setList([]);
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setList: function(thisData){			
			this.listData = thisData;
			this._HTMLDom["$count"].html(this.listData.length);
			this.isNoData();
			this.display();
		}
		, setMemberInfo: function(n, info, i){
			var $layer = this.$getLayer();
			var thisClass = this;
			i = i || "";
			
			var $o = $layer.find("tbody tr").eq(n).find("[data-param='닉네임" + i + "']");
			
			this.listData[n]["인자"]["회원정보"] = info;
			var d = this.listData[n]["인자"]["회원정보"];
			$o.hover(function(){
				_gf.showProfileLayer($(this), d, "left", 0, $(this).height());
			}, function(){
				_gf.closeProfileLayer();
			});
		}
		, display: function(){
			var $layer = this.$getLayer();
			var $element = this._HTMLDom["$element"];
			var thisClass = this;
			
			$layer.find("article, table").hide();
			
			if(this.listData.length){
				$layer.find("table").show();
				$layer.find("tbody").empty();
			
				for(var i=0;i<this.listData.length;++i){
					$layer.find("tbody").append($element.clone(true));	
				}
				
				$layer.find("tbody tr").each(function(n){
					var d = thisClass.listData[n];
					
					_gf.$getAlim({
						"템플릿코드": d["템플릿코드"]
						, "parent": this
						, "d": d
						, "n": n
						, "콜백": function($d, arg){
							thisClass.listData[n]["인자"] = arg["인자"];
							$(arg["parent"]).find("td:eq(0)").empty().append($d);
							
							var d = thisClass.listData[n]["인자"];
							
							for(var ii=0;ii<3;ii++){
								var iii = ii || "";
								
								if(d["회원아이디" + iii]){
									_gf.getMemberInfo({
										"아이디": d["회원아이디" + iii]
										, "iii": iii
										, "콜백": function(res, info, argg){
											thisClass.setMemberInfo(n, info, argg["iii"]);
										}
									});
								}
							}
						}
					});
					
					if(thisClass.a){
						$(this).find("td:eq(1)").html(d["경과시간"]);
					}else{
						$(this).find("td:eq(1)").html(d["등록정보"]["일자"]);
						$(this).find("td:eq(2)").html(d["등록정보"]["시간"]);
					}
				});
			}else{
				$layer.find("article").show();
			}
		}
		, isNoData: function(){
			if(!this.listData.length) {
				this._HTMLDom["$empty"].show();
			}else {
				this._HTMLDom["$empty"].hide();
			}
		}
	}
	
	function clsClubNewList(){
		this._HTMLDom = null;
		this.clubNewList = null;
		
		this._init();	
	}	
	
	clsClubNewList.prototype = {
		_init: function(){
			var $layer = $("div#container section#main_content article.club_new>table>tbody");
			
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("tr:first")
				, "$sub_layer": $layer.find("td.open").parent()
				, "$sub_element": $layer.find("td.open tr:first")
				, "$empty": $("body").find("article.board_list_none:eq(1)").html("등록된 글이 없습니다.")
			}
			
		}
		, setData: function(listData){
			this.clubNewList = listData;	
			this.display();
		}	
		, addProfile: function(n1,n2,oneData){			
			this.clubNewList[n1]["글목록"][n2]["프로필"] = oneData;
		}
		, display: function(){
			var thisClass = this;
			
			var $layer = this._HTMLDom["$layer"];
			var $element = this._HTMLDom["$element"];
			var $sub_layer = this._HTMLDom["$sub_layer"];
			var $sub_element = this._HTMLDom["$sub_element"];
	
			$layer.empty();
			
			this.isNoData(this.clubNewList.length);
			
			for(var i=0; i<this.clubNewList.length; ++i){
				$layer.append($element.clone(true));
			}
			
			$layer.find("tr").each(function(n){
				
				var obj = thisClass.clubNewList;
				
				$(this).find("td.name a").html(thisClass.clubNewList[n]["동호회명"]).click(function(event){
					thisClass.setIdx(n);
				});
				$(this).find("td p.new").html(thisClass.getFormatNumber(thisClass.clubNewList[n]["새글수"]));
				$(this).find("td p.rate").html(thisClass.getFormatNumber(thisClass.clubNewList[n]["회원수"]));
				$(this).find("td.align_c").html(thisClass.clubNewList[n]["등록시간"]);
				
				
				if(obj[n]){
					var $board = $(this).after($sub_layer.clone(true)).next();
					
					$board.find("tbody").empty();
										
					for(var i=0;i<obj[n]["글목록"].length;++i){
						$board.find("tbody").append($sub_element.clone(true));
					}
					
					if(obj[n]["글목록"].length){					
						$board.find("tr").each(function(v){
														
							$(this).find("td.align_l").css("cursor", "pointer").html(obj[n]["글목록"][v]["글제목"]).click(function(event){
								document.location.href = "../self_club/home.html?club_seq=" + obj[n]["동호회키"] + "&board_seq=" + obj[n]["글목록"][v]["게시판키"];
								//-->
							});
							
							if(obj[n]["글목록"][v]["댓글수"]) $(this).find("td.align_l").append("<em>(" + obj[n]["글목록"][v]["댓글수"] + ")</em>");
							
							$(this).find("td.point_b").html(obj[n]["글목록"][v]["닉네임"]).hover(function(event){
								_gf.showProfileLayer($(this), obj[n]["글목록"][v]["프로필"], "left", 0, $(this).height());
							}, function(event){
								_gf.objProfileLayer.hide();
							});
							$(this).find("td:last").html(obj[n]["글목록"][v]["등록시간"]);
							
						});
					}
				}
			});
			
			$layer.find("td.open").not(":first").hide();
			$layer.find("tr:first").removeClass("down").addClass("up");
						
		}
		, spinStart: function(){
			var $layer = this._HTMLDom["$layer"];
			$layer.hide();
			
			if($layer.parent().parent().prev("#tmpSpin").length == 0){
				$layer.parent().parent().before("<div id='tmpSpin' style='position:absolute; left:350px; top:600px;'></div>");
			}
			$layer.parent().parent().prev("#tmpSpin").spin();
						
		}
		, spinEnd: function(){
			var $layer = this._HTMLDom["$layer"];
			$layer.show();
			$layer.parent().parent().prev("#tmpSpin").spin(false);
			
		}
		, setIdx: function(val){
			var $layer = this._HTMLDom["$layer"];
						
			$layer.find(".up,.down").removeClass("up").removeClass("down").addClass("down").eq(val).removeClass("down").addClass("up");
			
			$layer.find(".open").hide().eq(val).show();
		}
		, getFormatNumber: function(n){
			n = String(n);			
			i = n.length;
			r = "";
			while(true){				
				if(i<=3) {
					r = n.substr(0,i) + r;					
					break;
				}
				r = ","+ n.substr(i-3,3) + r;				
				i-=3;
			}
			return r;
		}
		, isNoData: function(){
			if(!this.clubNewList.length) {
				this._HTMLDom["$empty"].show();
				$("div#idClubNewListPage").hide();
			}else {
				this._HTMLDom["$empty"].hide();
				$("div#idClubNewListPage").show();
			}
		}
	}
	
	//동호회라운드
	function clsClubScheduleList(){
		this._HTMLDom = null;
		this.scheduleList = null;
		this._init();
	}
	
	clsClubScheduleList.prototype = {		
		_init: function(){
			var $layer = $("div#container section#main_content article.list01 table:eq(0) tbody");
			
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("tr:first")
				, "$empty": $("body").find("article.board_list_none:eq(2)").html("등록된 글이 없습니다.")
			}
			
			$layer.empty();
		}	
		, setList: function(thisData){
			this.listData = thisData;
			this.display();			
		}
		, display: function(){
			var thisClass = this;
			var $layer = this._HTMLDom["$layer"];
			var $element = this._HTMLDom["$element"];
			
			$layer.empty();
			this.isNoData();
			
			for(var i=0;i<this.listData.length;++i){
				$layer.append($element.clone(true));	
			}
											
			$layer.find("tr").each(function(n){
				$(this).find("td:eq(0)").css("cursor", "pointer").html(thisClass.listData[n]["동호회명"]).click(function(event){
					event.preventDefault();
					document.location.href = "../self_club/home.html?club_seq=" + thisClass.listData[n]["키"] + "&round_seq=" + thisClass.listData[n]["라운드키"];
					//-->
				});
				$(this).find("td.day").html(thisClass.getFormatDateYmd(thisClass.listData[n]["라운드정보"]["일자"],". ") + "(" + thisClass.listData[n]["라운드정보"]["요일"] + ")");
				$(this).find("td:eq(2) strong").html(thisClass.listData[n]["라운드정보"]["코스명"]);
				$(this).find("td:eq(2)").contents().filter(function(){
					return (this.nodeType==3&&this.data.trim());
				}).after("").remove();
				
				//진행-P,확정-C,마감-D
				if(thisClass.listData[n]["라운드정보"]["진행상태"]=="P"){
					$(this).find("td:eq(3)").attr("src", "../images/btn/btn_ing.png");
				}else if(thisClass.listData[n]["라운드정보"]["진행상태"]=="C"){
					$(this).find("td:eq(3)").attr("src", "../images/btn/btn_ok.png");
				}else if(thisClass.listData[n]["라운드정보"]["진행상태"]=="D"){
					$(this).find("td:eq(3)").attr("src", "../images/btn/btn_end.png");
				}
				
				$(this).find("td:eq(4)").html(thisClass.listData[n]["제목"]);
			});
			
		}
		, spinStart: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.hide();
			if($layer.parent().parent().prev("#tmpSpin").length == 0){
				$layer.parent().parent().before("<div id='tmpSpin' style='position:absolute; left:350px; top:450px;'></div>");
			}
			$layer.parent().parent().prev("#tmpSpin").spin();
		}
		, spinEnd: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.parent().parent().prev("#tmpSpin").spin(false);
			$layer.show();
		}
		, getFormatDateYmd: function (n,v){			
			n = String(n);
			
			return n.substr(0,4) +v+ n.substr(4,2) +v+ n.substr(6,2);
		}
		, getFormatNumber: function(n){
			n = String(n);			
			i = n.length;
			r = "";
			while(true){				
				if(i<=3) {
					r = n.substr(0,i) + r;					
					break;
				}
				r = ","+ n.substr(i-3,3) + r;				
				i-=3;
			}
			return r;
		}
		, isNoData: function(){
			if(!this.listData.length) {
				this._HTMLDom["$empty"].show();
				$("div#idClubScheduleListPage").hide();
			}else {
				this._HTMLDom["$empty"].hide();
				$("div#idClubScheduleListPage").show();
			}
		}
	}
	
		
	// 서버통신
	function clsComm()
	{
		//초기화
		this.clubNotificationList = null;
		this.clubNotificationListPageData = null;
		
		this.clubNewList = null;
		this.clubNewListPageData = null;
		this.clubNewListRequest = null;
				
		this.clubScheduleList = null;
		this.clubScheduleListPageData = null;
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
//------------------------ 동호회새글------------------------//						
			this.clubNewListRequest = {
				"페이지": null
			}	
//------------------------ 동호회알림------------------------//		
			this.clubNotificationList = {
				"동호회키": null
				, "동호회명": null
				, "알림구분": null
				, "알림구분명": null
				, "알림내용": null
				, "승인유무": null
				, "알림시간": null
			}
			
			this.clubNotificationListRequest = {
				"페이지": null
			}			
//------------------------ 동호회라운드------------------------//		
			this.scheduleClubPageData = {
				"전체수":null
			}
			
			this.scheduleClubRequest = {
				 "페이지": null				
			}
//------------------------ 동호회라운드------------------------//		

		}
//------------------------ 동호회새글------------------------//			
		//동호회새글
		, getClubNewList: function(){
			var thisClass = this;
			
			var szPage = this.getClubNewListRequest("페이지");
			
			// ajax 시작			
			//parameter setting and run
			_gf.ajax({
				"sid":"CL001",
				"servicename":"getMyClubBoardList",
				"listcount":5,
				"currpageno":szPage
			}, {
				  
			}, function(ret){
				
				thisClass.clubNewListPageData = {
					"전체수": ret["msp"]["contents"]["@totalcount"]
				}
				
				// callback and mapping
				thisClass.clubNewList = [];
			
			
				if(ret["msp"]["contents"]["items"]){
					
					if(!ret["msp"]["contents"]["items"].length){
						ret["msp"]["contents"]["items"] = new Array(ret["msp"]["contents"]["items"]);
					}
						
						
					for(var i=0;i<ret["msp"]["contents"]["items"].length;i++){
						var arr = [];
						var obj = ret["msp"]["contents"]["items"][i];
						
						//글목록
						if(!obj["clubBoardList"][0]["items"].length){
							obj["clubBoardList"][0]["items"] = new Array(obj["clubBoardList"][0]["items"]);
						}
						
						//글목록
						for(var k=0;k<obj["clubBoardList"][0]["items"].length;++k){							
							var objList = obj["clubBoardList"][0]["items"][k];
							
							arr.push( {
									"게시판키":objList["board__seq"]
									, "글제목":  objList["title"]
									, "댓글수": objList["cr__cnt"]
									, "아이디": objList["reg__id"]
									, "닉네임": objList["reg__nickname"]
									, "등록시간": objList["timelag"]	
									, "프로필": []
							});								
						}
						
						//동호회목록
						thisClass.clubNewList.push({
							"동호회키": obj["clubSeq"]
							, "동호회명": obj["clubName"]
					
							, "글목록": arr
					
							, "새글수": obj["newBoardCnt"]
							, "회원수": obj["memberCnt"]
							, "등록시간": obj["timelag"]
						});
						
					}                                   
				}
				
				$(thisClass).trigger("evtFinishClubNewList");
			});
			// ajax 끝
			                                                                                                     
		}
		, getClubNewListData: function(){
			return this.clubNewList;
		}
		, setClubNewListRequest: function(key, val){
			this.clubNewListRequest[key] = val;	
		}
		, getClubNewListRequest: function(key){
			return this.clubNewListRequest[key];	
		}
		, getClubNewListPageData: function(){
			return this.clubNewListPageData;	
		}
//------------------------ 동호회새글------------------------//						
//------------------------ 라운드동호회-----------------------------//		
		, getClubScheduleList: function(){
			var thisClass = this;
			var szPage = this.scheduleClubRequest["페이지"];
			
			// ajax 시작			
			//parameter setting and run
			_gf.ajax({
				"sid":"CL001",
				"servicename":"getClubRoundList",
				"listcount":5,
				"currpageno":szPage
			}, {
				"clubSeq":null
			}, function(ret){
				
				thisClass.scheduleClubPageData = {
					"전체수": ret["msp"]["contents"]["@totalcount"]
				}
				
				// callback and mapping
				thisClass.scheduleClubList = [];
								
				if(ret["msp"]["contents"]["items"]){
					if(!ret["msp"]["contents"]["items"].length){
						ret["msp"]["contents"]["items"] = new Array(ret["msp"]["contents"]["items"]);
					}
					for(var i=0;i<ret["msp"]["contents"]["items"].length;i++){
						
						thisClass.scheduleClubList.push({
							"키": ret["msp"]["contents"]["items"][i]["roundInfo"]["clubSeq"]
							, "라운드키": ret["msp"]["contents"]["items"][i]["roundInfo"]["roundSeq"]
							, "동호회명": ret["msp"]["contents"]["items"][i]["clubName"]
							, "라운드정보": { 
								"일자":ret["msp"]["contents"]["items"][i]["roundInfo"]["reservDate"]
								, "요일": ret["msp"]["contents"]["items"][i]["roundInfo"]["reservDay"]
								,"코스명":ret["msp"]["contents"]["items"][i]["roundInfo"]["golfclubCourseInfo"]
								,"진행상태":ret["msp"]["contents"]["items"][i]["roundInfo"]["procStatus"]
							}
							, "제목": ret["msp"]["contents"]["items"][i]["roundInfo"]["title"]
						});
					}
				}
				
				$(thisClass).trigger("evtFinishClubScheduleList");
			});
			// ajax 끝			
		}
		, getClubScheduleListData: function(){
			return this.scheduleClubList;
		}
		, getClubScheduleListPageData: function(){
			return this.scheduleClubPageData;
		}
		, setClubScheduleListRequest: function(key, val){
			this.scheduleClubRequest[key] = val;
		}
		, getClubScheduleListRequest: function(key){
			return this.scheduleClubRequest[key];
		}		
//------------------------ 라운드동호회-----------------------------//
	}
	
	// 초기화면 제어
	function clsControl()
	{
		
		//초기화
		this.objComm = null;
		
		this.objClubNotificationList = null;
		
		this.objClubNewList = null;
		this.objClubNewListPage = null;
		
		this.objClubScheduleList = null;
		this.objClubScheduleListPage = null;
		
		this._init();
	}
	
	clsControl.prototype = {
				
		//초기화
		_init: function(){
			var thisClass = this; 
			this.objComm = new clsComm();
			
			this.objClubNotificationList = new clsClubNotificationList();
			
			this.objClubNewList = new clsClubNewList();
			this.objClubNewListPage = new _gf.makeTinyPaging($("#idClubNewListPage"), 5)
			
			this.objClubScheduleList = new clsClubScheduleList();
			this.objClubScheduleListPage = new _gf.makeTinyPaging($("#idClubScheduleListPage"), 5)
			
			this._bind();			
			
			// 동호회 받은 알림
			_gf.getAlimList({
				"알림구분코드": "N"
				, "조회기간": 15
				, "상단알림여부": "N"
				, "서비스구분코드": "CL"
				, "콜백": function(ret){
					thisClass.finishTodayAlimList(ret);
				}
			});
			
			//동호회 새글 조회
			this.objClubNewList.spinStart();
			this.objComm.setClubNewListRequest("페이지",1);
			this.objClubNewListPage.setPage(1);
			this.objComm.getClubNewList();
			
			//동호회 라운드 조회
			this.objClubScheduleList.spinStart();
			this.objComm.setClubScheduleListRequest("페이지",1);
			this.objClubScheduleListPage.setPage(1);
			this.objComm.getClubScheduleList();
		}
		
		//데이터 화면처리
		, _bind: function(){
			var thisClass = this;
			
			//전체동호회 버튼 이벤트
			$("div#container section#main_content article.depth2_tab ul li a img[alt=전체동호회]").click(function(event){
				event.preventDefault();					
				document.location.href = "../club/club.html";
			});
			
			//나의동호회 버튼 이벤트
			$("div#container section#main_content article.depth2_tab ul li a img[alt=나의동호회]").click(function(event){
				event.preventDefault();					
				document.location.href = "../club/club_join.html";
			});
			
			//나의동호회알림 버튼 이벤트
			$("div#container section#main_content article.club_myclub ul li a:eq(1)").click(function(event){
				event.preventDefault();					
				document.location.href = "../club/club_my.html";
			});
			
			//나의동호회알림 버튼 이벤트
			$("div#container section#main_content article.club_myclub ul li a:eq(0)").click(function(event){
				event.preventDefault();					
				document.location.href = "../club/club_join.html";
			});
			
			//동호회개설 버튼 이벤트
			$(".btn_depth a img").click(function(event){
					event.preventDefault();					
					document.location.href = "../club/club_creat.html";
			});
			
			//동호회알림 조회 완료 이벤트
			$(this.objClubMyList).bind("evtFinishClubNotificationList", function(){ 
				var thisData = this.getClubNotificationListData();
				var pageData = thisClass.objComm.getClubNotificationListPageData();
				
				thisClass.objClubNotificationList.spinEnd();
				thisClass.objClubNotificationList.setData(thisData);
				
				//페이징
				thisClass.objClubNotificationListPage.setTotalCount(pageData["전체수"]);
				thisClass.objClubNotificationListPage.display();
			});
			
			//동호회새글 조회 완료 이벤트
			$(this.objComm).bind("evtFinishClubNewList", function(){
				var thisData = this.getClubNewListData();
				var pageData = thisClass.objComm.getClubNewListPageData();
				
				thisClass.objClubNewList.spinEnd();
				thisClass.objClubNewList.setData(thisData);
				
				//페이징
				thisClass.objClubNewListPage.setTotalCount(pageData["전체수"]);
				thisClass.objClubNewListPage.display();
	
				for(var i=0;i<thisData.length;++i){					
					for(var k=0;k<thisData[i]["글목록"].length;++k){
						//프로필
						_gf.getMemberInfo({
							"아이디": thisData[i]["글목록"][k]["아이디"]
							, "인덱스": i
							, "글인덱스": k
							, "콜백": function(res, info, arg){
								thisClass.objClubNewList.addProfile(arg["인덱스"],arg["글인덱스"], info);
							}
						});
					}
				}
				
			});
			
			// 동호회라운드 조회 완료이벤트
			$(this.objComm).bind("evtFinishClubScheduleList", function(){
				var thisData = this.getClubScheduleListData();
				var pageData = thisClass.objComm.getClubScheduleListPageData();
				
				thisClass.objClubScheduleList.spinEnd();
				thisClass.objClubScheduleList.setList(thisData);
				thisClass.objClubScheduleListPage.setTotalCount(pageData["전체수"]);
				thisClass.objClubScheduleListPage.display();
			});			
			// 동호회라운드 페이지이동 이벤트
			$(this.objClubScheduleListPage).bind("evtPageClick", function(){
				var thisPage = this.getPage();
				
				thisClass.objComm.setClubScheduleListRequest("페이지", thisPage);
				thisClass.objClubScheduleList.spinStart();
				thisClass.objComm.getClubScheduleList();
			});
			
			//동호회알림 페이지이동 이벤트
			$(this.objClubNewListPage).bind("evtPageClick", function(){
				var thisPage = this.getPage();
				
				thisClass.objComm.setClubNotificationListRequest("페이지", thisPage);
				thisClass.objClubNotificationList.spinStart();
				thisClass.objComm.getClubNotificationList();
				
			});
			
			//동호회새글 페이지이동 이벤트
			$(this.objClubNewListPage).bind("evtPageClick", function(){
				var thisPage = this.getPage();
				
				thisClass.objComm.setClubNewListRequest("페이지", thisPage);
				thisClass.objClubNewList.spinStart();
				thisClass.objComm.getClubNewList();
				
			});
				
		}
		// 오늘받은 알림 완료
		, finishTodayAlimList: function(ret){
			this.objClubNotificationList.setList(ret["결과값"]);
		}
					
	}
	
	document.objControl = new clsControl();
});